// DlgEditCollection.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgEditCollection.h"
#include "DlgPickList.h"


// CDlgEditCollection dialog

IMPLEMENT_DYNAMIC(CDlgEditCollection, CDialog)

CDlgEditCollection::CDlgEditCollection(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEditCollection::IDD, pParent)
	, m_szID(_T(""))
	, m_szDescription(_T(""))
	, m_szName(_T(""))
	, m_bUserListed(FALSE)
	, m_bElectronic(FALSE)
	, m_szIconFile(_T(""))
	, m_szPrintNotice(_T(""))
{
	m_bAddCollection = FALSE;
	m_nID = -1;
	m_nIndexID = -1;
}

CDlgEditCollection::~CDlgEditCollection()
{
}

void CDlgEditCollection::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_ID, m_szID);
	DDX_Text(pDX, IDC_EDIT_DESCRIPTION, m_szDescription);
	DDX_Text(pDX, IDC_EDIT_NAME, m_szName);
	DDX_Check(pDX, IDC_CHECK_USER_LISTED, m_bUserListed);
	DDX_Control(pDX, IDC_SPIN_RECORD, m_ctrlRecord);
	DDX_Check(pDX, IDC_CHECK_ELECTRONIC, m_bElectronic);
	DDX_Text(pDX, IDC_EDIT_ICON_FILE, m_szIconFile);
	DDX_Text(pDX, IDC_EDIT_PRINT_NOTICE, m_szPrintNotice);
}


BEGIN_MESSAGE_MAP(CDlgEditCollection, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_APPLY, &CDlgEditCollection::OnBnClickedButtonApply)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, &CDlgEditCollection::OnBnClickedButtonDelete)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_RECORD, &CDlgEditCollection::OnDeltaposSpinRecord)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CDlgEditCollection::OnBnClickedButtonBrowse)
END_MESSAGE_MAP()


// CDlgEditCollection message handlers

BOOL CDlgEditCollection::OnInitDialog()
{
	CDialog::OnInitDialog();

	// fetch collection
	CStringArray szaCollection;
	COLLECTION_LOOKUP Collection;
	appConfig.m_SQL.GetDescriptions( CMySQLEx::collection, &szaCollection, CMySQLEx::list, &m_naID );

	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );

	// check if adding
	if( m_bAddCollection )
	{
		return TRUE;
	}
	else
	{
		GetDlgItem( IDC_EDIT_ID )->EnableWindow( FALSE );
	}

	// pick list
	if( m_nID < 0 )
	{
		// show pick list
		CDlgPickList PickListDlg;
		PickListDlg.m_pszaStrings = &szaCollection;
		if( PickListDlg.DoModal() == IDOK )
			m_nID = _tstol( PickListDlg.m_szString.Left( PickListDlg.m_szString.Find( _T(" ") )));
		else
		{
			EndDialog( IDCANCEL );
			return TRUE;
		}
	}

	// get collection
	if( !appConfig.m_SQL.GetDescriptionCollection( &Collection, m_nID ))
	{
		EndDialog( IDCANCEL );
		return TRUE;
	}

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = Collection.szDescription;
	m_szName = Collection.szName;
	m_szPrintNotice = Collection.szPrintNotice;
	m_bUserListed = Collection.nUserListed;
	m_bElectronic = Collection.nElectronic;
	m_szIconFile = Collection.szIconFile;

	// set index
	for( INT nIndex = 0; nIndex < m_naID.GetCount(); nIndex++ )
		if( m_nID == m_naID.GetAt( nIndex ))
		{
			m_nIndexID = nIndex;
			m_ctrlRecord.SetPos( nIndex );
			break;
		}
	// no index found
	if( m_nIndexID < 0 )
		m_ctrlRecord.EnableWindow( FALSE );

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgEditCollection::OnOK()
{
	// update record
	UpdateRecord();
	// clsoe
	CDialog::OnOK();
}

void CDlgEditCollection::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnCancel();
}

void CDlgEditCollection::OnBnClickedButtonApply()
{
	// update record
	UpdateRecord();
}

void CDlgEditCollection::OnBnClickedButtonDelete()
{
	// update data
	UpdateData();

	// get collection id
	m_nID = _tstol( m_szID );

	// check for 0
	if( m_nID == 0 )
	{
		CString szMessage;
		AfxFormatString1( szMessage, IDS_ERROR_DELETE_ITEM_0, _T("Collection") );
		AfxMessageBox( szMessage );
		return;
	}
	// check if sure
	if( AfxMessageBox( IDS_DELETE_COLLECTION, MB_YESNO ) == IDNO )
		return;

	// remove row
	appConfig.m_SQL.RemoveRow( CMySQLEx::collection, m_nID );

	// adjust requests
	appConfig.m_SQL.UpdateFieldValue( CMySQLEx::requests, _T("collection_id"), m_nID, 0 );

	// remove from array
	m_naID.RemoveAt( m_nIndexID );
	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );
	// reset index
	m_nIndexID = 0;
	m_nID = m_naID.GetAt( m_nIndexID );

	COLLECTION_LOOKUP Collection;
	// get collection
	if( !appConfig.m_SQL.GetDescriptionCollection( &Collection, m_naID.GetAt( m_nIndexID )) )
	{
		EndDialog( IDCANCEL );
		return;
	}

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = Collection.szDescription;
	m_szName = Collection.szName;
	m_szPrintNotice = Collection.szPrintNotice;
	m_bUserListed = Collection.nUserListed;
	m_bElectronic = Collection.nElectronic;
	m_szIconFile = Collection.szIconFile;

	//update data
	UpdateData( FALSE );
}

void CDlgEditCollection::OnDeltaposSpinRecord(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);

	// increment index
	m_nIndexID += pNMUpDown->iDelta;
	if( m_nIndexID > m_naID.GetUpperBound() )
		m_nIndexID = 0;
	if( m_nIndexID < 0 )
		m_nIndexID = (INT)m_naID.GetUpperBound();

	COLLECTION_LOOKUP Collection;
	// get collection
	if( !appConfig.m_SQL.GetDescriptionCollection( &Collection, m_naID.GetAt( m_nIndexID )))
	{
		EndDialog( IDCANCEL );
		return;
	}

	// get id
	m_nID = Collection.nID;

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = Collection.szDescription;
	m_szName = Collection.szName;
	m_szPrintNotice = Collection.szPrintNotice;
	m_bUserListed = Collection.nUserListed;
	m_bElectronic = Collection.nElectronic;
	m_szIconFile = Collection.szIconFile;

	//update data
	UpdateData( FALSE );

	*pResult = 0;
}

BOOL CDlgEditCollection::UpdateRecord()
{
	// update data
	UpdateData();

	// get id
	m_nID = _tstol( m_szID );

	// check data
	if( m_szID.IsEmpty() ||	m_szDescription.IsEmpty() || m_szName.IsEmpty() )
	{
		AfxMessageBox( _T("Data missing.\n\nPlease complete all data fields.") );
		return FALSE;
	}

	// check for overwrite
	if( m_bAddCollection )
	{
		if( appConfig.m_SQL.GetRowCount( CMySQLEx::collection, m_nID ) > 0 )
		{
			if( AfxMessageBox( IDS_CHANGE_COLLECTION, MB_YESNO ) == IDNO )
			{
				// return
				return FALSE;
			}
		}
	}

	CStringArray szaCollection;
	COLLECTION_LOOKUP Collection;

	// set variables
	Collection.nID = m_nID;
	Collection.szDescription = m_szDescription;
	Collection.szName = m_szName;
	Collection.szPrintNotice = m_szPrintNotice;
	Collection.nUserListed = m_bUserListed;
	Collection.nElectronic = m_bElectronic;
	Collection.szIconFile = m_szIconFile;

	// save it
	appConfig.m_SQL.SetDescriptionCollection( &Collection, m_nID, m_bAddCollection );

	// fetch collection
	appConfig.m_SQL.GetDescriptions( CMySQLEx::collection, &szaCollection, CMySQLEx::list, &m_naID );

	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );
	// set index
	for( INT nIndex = 0; nIndex < m_naID.GetCount(); nIndex++ )
		if( m_nID == m_naID.GetAt( nIndex ))
		{
			m_nIndexID = nIndex;
			m_ctrlRecord.SetPos( nIndex );
			break;
		}
	// no index found
	if( m_nIndexID < 0 )
		m_ctrlRecord.EnableWindow( FALSE );

	// update data
	UpdateData( FALSE );
	// return true
	return TRUE;
}

void CDlgEditCollection::OnBnClickedButtonBrowse()
{
	// open ico or bmp file	
	TCHAR szFilter[] = _T("Icon Files (*.ico)|*.ico|Bitmap Files (*.bmp)|*.bmp|All Files (*.*)|*.*||");
	CFileDialog fileDlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter );
	// show dialog
	if( fileDlg.DoModal() == IDOK )
	{
		m_szIconFile = fileDlg.GetPathName();
		// update data
		UpdateData( FALSE );
	}
}
